package com.camel.docker;

import com.camel.docker.listener.ApplicationStartUpListener;
import com.camel.docker.config.FTPClientFactory;
import com.camel.docker.config.FTPClientPool;
import com.camel.docker.config.FTPConfig;
import com.camel.docker.service.DockerClientService;
import com.camel.docker.utils.FTPUtils;
import com.camel.docker.utils.PropertyUtil;
import com.camel.docker.utils.ShutdownContext;
import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.model.Info;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.connector.Connector;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
import org.springframework.boot.web.server.Shutdown;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

@Slf4j
@SpringBootApplication
public class DockerMonitorApplication implements CommandLineRunner, TomcatConnectorCustomizer{

    public static void main(String[] args) {
        SpringApplication.run(DockerMonitorApplication.class, args).addApplicationListener(new ApplicationStartUpListener());
    }

    @Autowired
    public DockerClientService dockerClientService;

    @Autowired
    private FTPConfig ftpProperties;

    @Autowired
    private ShutdownContext shutdownContext;

    private volatile Connector connector;

    @Bean
    public RestTemplate restTemplate() {
        //复杂构造函数的使用
        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        requestFactory.setConnectTimeout(300000);// 设置超时
        requestFactory.setReadTimeout(300000);
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.setRequestFactory(requestFactory);
        return restTemplate;
    }

    @Override
    public void run(String... args) throws Exception {
        DockerClient dockerClient = dockerClientService.connect();
        FTPClientFactory factory = new FTPClientFactory(ftpProperties);
        try {
            FTPUtils.ftpClientPool = new FTPClientPool(factory);
        } catch (Exception e) {
            log.error("FTP服务器连接失败，请检查配置");
            shutdownContext.showdown();
        }

        try {
            Info info = dockerClient.infoCmd().exec();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("DOCKER服务器连接失败，请检查配置");
            shutdownContext.showdown();
        }
    }

    @Override
    public void customize(Connector connector) {
        this.connector = connector;
    }
}
